package com.tedu.dao.impl;

import com.tedu.dao.UserDao;
import com.tedu.entry.User;
import com.tedu.util.JDBCUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Author：毕龙
 * Date: 2023/7/21 15:54
 */
public class UserDaoImpl implements UserDao {
    @Override
    public int insert(User user) {
        Connection conn = null;
        PreparedStatement pstm = null;
        try {
            //1.获取链接
            conn = JDBCUtil.getConnection();
            //2.执行sql
            String sql = "insert into user(username,password,birthday,create_time) values(?,?,?,?)";
            //给？注入值
            pstm = conn.prepareStatement(sql);
            pstm.setString(1,user.getUsername());
            pstm.setString(2, user.getPassword());
            pstm.setDate(3,new Date(user.getBirthday().getTime()));
            pstm.setDate(4,new Date(user.getCreateTime().getTime()));
            int i = pstm.executeUpdate();
            System.out.println("添加了一条数据");
            return i;

        } catch (Exception e) {

        } finally {
            //释放资源
            JDBCUtil.release(null,pstm,conn);

        }
        return 0;
    }

    @Override
    public int delete(int id) {
        Connection conn = null;
        PreparedStatement pstm = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = "delete from user where id = ?";
            pstm = conn.prepareStatement(sql);

            pstm.setInt(1,id);
            int i = pstm.executeUpdate();
            System.out.println("影响了"+i+"行");

        } catch (Exception e) {

        } finally {
            JDBCUtil.release(null,pstm,conn);
        }


        return 0;
    }

    @Override
    public int update(User user) {
        Connection conn = null;
        PreparedStatement pstm = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = "update user set username = ?,password = ? where id = ?";
            pstm = conn.prepareStatement(sql);

            pstm.setString(1,user.getUsername());
            pstm.setString(2, user.getPassword());
            pstm.setInt(3,user.getId());

            int i = pstm.executeUpdate();
            System.out.println("影响了"+i+"行");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.release(null,pstm,conn);
        }

        return 0;
    }

    @Override
    public User get(int id) {
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        User user = null;
        try {
            //1.获取链接
            conn = JDBCUtil.getConnection();
            //2.执行sql
            String sql = "select id,username,password,birthday,create_time from user where id = ?";
            pstm = conn.prepareStatement(sql);
            pstm.setInt(1,id);
            //3.处理查询结果
            rs = pstm.executeQuery();//查询 ResultSet
            //pstm.executeUpdate()//增删改
            if (rs.next()){
                int id1 = rs.getInt(1);
                String username = rs.getString(2);
                String password = rs.getString(3);
                Date birthday = rs.getDate(4);
                Date createTime = rs.getDate(5);
                //创建一个User对象
                user = new User(id1, username, password, birthday, createTime);

            }


        } catch (Exception e) {

        } finally {
            //释放资源
            JDBCUtil.release(rs,pstm,conn);

        }
        return user;
    }

    @Override
    public List<User> list() {
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        ArrayList<User> list = new ArrayList<>();
        try {
            //1.获取链接
            conn = JDBCUtil.getConnection();
            //2.执行sql
            String sql = "select id,username,password,birthday,create_time from user";
            pstm = conn.prepareStatement(sql);
            //3.处理查询结果
            rs = pstm.executeQuery();//查询 ResultSet
            //pstm.executeUpdate()//增删改
            while (rs.next()){
                int id = rs.getInt(1);
                String username = rs.getString(2);
                String password = rs.getString(3);
                Date birthday = rs.getDate(4);
                Date createTime = rs.getDate(5);
                //创建一个User对象
                User user = new User(id, username, password, birthday, createTime);
                list.add(user);
            }


        } catch (Exception e) {

        } finally {
            //释放资源
            JDBCUtil.release(rs,pstm,conn);

        }


        return list;
    }
}
